package main.collection;

import com.google.gson.Gson;

import java.io.Serializable;
import java.util.*;

/**
 * @author ChengGM
 * @create 2017-01-04 10:54
 **/
public class CollectionTest {


    public static void main(String[] args) {
        Function function = new Function();
        function.setName("管理首页");
        function.setId("01");
        function.setParentId("0");
        function.setLevel(1);
        Function function1 = new Function();
        function1.setName("系统管理");
        function1.setId("02");
        function1.setParentId("0");
        function1.setLevel(1);
        Function function2 = new Function();
        function2.setName("权限管理");
        function2.setId("0201");
        function2.setParentId("02");
        function.setLevel(2);
        Function function3 = new Function();
        function3.setName("添加管理员");
        function3.setId("0201000001");
        function3.setParentId("0201");
        function3.setLevel(3);
        Function function4 = new Function();
        function4.setName("管理员日志");
        function4.setId("0201000003");
        function4.setParentId("0201");
        function4.setLevel(3);
        Function function5 = new Function();
        function5.setName("删除管理员");
        function5.setId("0201000002");
        function5.setParentId("0201");
        function5.setLevel(3);

        List<Function>  list = new ArrayList<Function>();
        list.add(function);
        list.add(function1);
        list.add(function2);
        list.add(function3);
        list.add(function4);
        list.add(function5);


        List list2 = null;
//        sortList(list2,list,"0",true);
//        sortList(list2,list,"0",false);
        Gson gson = new Gson();
        System.out.println(gson.toJson(list2));
    }
    public static void oneLevel(List<Function> list,List<Function> resorucesList) {
        List<Function> levelOne = new ArrayList<Function>();
        for (Function function : resorucesList) {
            if (function.getLevel().equals(1)) {
                list.add(function);
            }
        }
        List<Function> levelTwo = new ArrayList<Function>();
        for (Function function : levelOne) {
            if (function.getLevel().equals(2)) {
                levelTwo.add(function);
            }
        }


        List<Function> levelThree = new ArrayList<Function>();
        for (Function function : levelOne) {
            if (function.getLevel().equals(3)) {
                levelThree.add(function);
            }
        }
        List<Function> levelFive = new ArrayList<Function>();
        List<Function> levelFour = new ArrayList<Function>();
        if (!levelThree.isEmpty()) {
            for (Function function : levelThree) {
                if (function.getLevel().equals(4)) {
                    levelFour.add(function);
                }
            }

            for (Function function : levelFour) {
                if (function.getLevel().equals(5)) {
                    levelFive.add(function);
                }
            }
        }

        if (!levelOne.isEmpty() || levelOne.size() > 0) {
            for (Function one : levelOne) {
                for (Function two : levelTwo) {

                    if (one.getId().equals(two.getParentId())) {
                        one.getChild().add(two);
                    }
                }
            }
        }

if (!levelFour.isEmpty() || levelFour.size() > 0) {
            for (Function four : levelFour) {
                for (Function five : levelFive) {

                    if (four.getId().equals(five.getParentId())) {
                        four.getChild().add(five);
                    }
                }
            }
        }

        if (!levelThree.isEmpty() || levelThree.size() > 0) {
            for (Function three : levelThree) {
                for (Function four : levelFour) {

                    if (three.getId().equals(four.getParentId())) {
                        three.getChild().add(four);
                    }
                }
            }
        }
    }
}



    /*    for (int i=0; i<sourcelist.size(); i++){
            Function e = sourcelist.get(i);
            if (e.getParent()!=null && e.getParent().getId()!=null
                    && e.getParent().getId().equals(parentId)){
                list.add(e);
                if (flag){
                    // 判断是否还有子节点, 有则继续获取子节点
                    for (int j=0; j<sourcelist.size(); j++){
                        Function child = sourcelist.get(j);
                        if (child.getParent()!=null && child.getParent().getId()!=null
                                && child.getParent().getId().equals(e.getId())){
                            sortList(list, sourcelist, e.getId(), true);
                            break;
                        }
                    }
                }
            }
        }*/




class Function implements Serializable{
    private List<Function> child;
    private String id;
    private String parentId;
    private String name;
    private Integer level;

    public List<Function> getChild() {
        return child;
    }

    public void setChild(List<Function> child) {
        this.child = child;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}